// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html


cc.Class({
    extends: cc.Component,



    properties: {
        rope_node : {
            default : null,
            type : cc.Node
        },
        cow_ins : {
            default: null,
            type : cc.Node
        },
        rope_images : {
            default : [],
            type : cc.SpriteFrame
        },
        cow_prefab : {
            default : null,
            type : cc.Prefab
        },
        time : 0,



    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //定义初始化得分
        this.success = false;
        this.scoreNum = 0;
        /*this.width = 0;
        this.height = 0;*/
    },

    start () {
        //获得计时器组件
        let countDownLabel = cc.find("Canvas/count_down").getComponent(cc.Label);
        countDownLabel.string = "倒计时"+this.time+"s";
        // let time = 60;
        this.schedule(function () {
            this.time--;
            countDownLabel.string = "倒计时"+this.time+"s";
            if(this.time === 0){
                //获取结果弹窗节点
                let resultNode = cc.find("Canvas/result");
                resultNode.setSiblingIndex(100);
                //获取子节点
                let titleNode = resultNode.getChildByName("title");
                let contentNode = resultNode.getChildByName("content");
                //获取Label组件
                let titleLabel = titleNode.getComponent(cc.Label);
                let contentLabel = contentNode.getComponent(cc.Label);
                //最终得分显示
                titleLabel.string = "最终得分"+this.scoreNum;
                //最终成就显示
                switch (true) {
                    case this.scoreNum <= 1:
                        contentLabel.string = "套牛菜鸟";
                        break;
                    case this.scoreNum <= 10:
                        contentLabel.string = "套牛高手";
                        break;
                    case this.scoreNum <= 20:
                        contentLabel.string = "套牛大师";
                        break;
                }
                resultNode.active = true;
                let topScore = this.scoreNum;
                // wx.login({
                //     success(res){
                //         if (res.code){
                //             //发起网络请求给后台
                //             wx.request({
                //                 url : "http://localhost:8080/ccbackstage_war/updateTopScore",
                //                 method : "POST",
                //                 header: {'content-type':'application/x-www-form-urlencoded'},
                //                 data: {
                //                     code:res.code,
                //                     topScore:topScore,
                //                 },
                //             });
                //         }else {
                //             console.log("保存失败"+res.errorMessage);
                //         }
                //     }
                // });
                cc.director.pause();
            }
        },1);

        //获取微信界面大小,


        /*wx.getSystemInfo({
            success (res) {
                this.width = res.screenWidth;
                console.log(this.width);
                this.height = res.screenHeight;
                console.log(this.height);

            }
        });*/
        let sysInfo = wx.getSystemInfoSync();
        //获取微信界面大小
        let width = sysInfo.screenWidth;
        let height = sysInfo.screenHeight;

        //创建一个用户授权按钮
       // const wxLoginBtn = wx.createUserInfoButton({
       //     type : "text",
       //     image : "",
       //     style: {
       //         left: 0,
       //         top: 0,
       //         width: width,
       //         height: height,
       //         lineHeight: 40,
       //         backgroundColor: '#00000000',
       //         color: '#00000000',
       //         textAlign: 'center',
       //         fontSize: 16,
       //         borderRadius: 4
       //     }
       //  });
       // console.log(this.width,this.height);
       console.log(width,height);
       let self = this;
        // wxLoginBtn.onTap((res)=>{
        //     // console.log(res.userInfo);
        //     //拿到用户信息
        //     let userInfo = res.userInfo;
        //     self.wxLogin(userInfo);
        //     //销毁按钮
        //     wxLoginBtn.destroy();
        //     //开始游戏
        //     cc.director.resume();
        // });
        // wx.getUserInfo({
        //     success(res) {
        //         // 此处可以获取到用户信息
        //         // console.log(res.userInfo);
        //         self.wxLogin(res.userInfo);
        //         wxLoginBtn.destroy();
        //         //开始游戏
        //         cc.director.resume();
        //     },
        //     fail(err) {
        //         console.log("接口调用失败！");
        //         //暂停游戏
        //         cc.director.pause();
        //     }
        // });
                //开始游戏
                cc.director.resume();
    },
    //微信加载图片
    // wxLogin(userInfo){
    //     let avatar = cc.find("Canvas/avatar").getComponent(cc.Sprite);
    //     //加载远程网络图片
    //     cc.loader.load({url:userInfo.avatarUrl,type:"png"},function (err,texture) {
    //         avatar.spriteFrame = new cc.SpriteFrame(texture);
    //     });
    //     wx.login({
    //         success(res){
    //             if(res.code){
    //                 //发起网络请求
    //                 wx.request({
    //                     url : "http://localhost:8080/ccbackstage_war/login",
    //                     method : "POST",
    //                     header : {
    //                         "content-type" : "application/x-www-form-urlencoded",
    //                     },
    //                     data : {
    //                         code : res.code,
    //                         nickName : userInfo.nickName,
    //                         avatarUrl : userInfo.avatarUrl,
    //                     }
    //                 })
    //             }else {
    //                 console.log("登陆失败！"+res.errorMessage);
    //             }


    //         }
    //     });
    // },



    // update (dt) {},
    clickCapture: function (event, customEventData) {
        this.rope_node.active = true;
        //设置绳子当前在父节点的顺序
        this.rope_node.setSiblingIndex(100);
        //设置绳子起始位置
        this.rope_node.y = -680;
        //向上移动
        const up = cc.moveTo(0.5, this.rope_node.x, -200);
        //判定结果
        let result = cc.callFunc(function () {
            //获取当前牛的x点
            let currentX = this.cow_ins.x;
            if(currentX > -50 && currentX < 50){
                cc.log("捕获成功");
                //移除牛儿
                // let bgNode = this.node.getChildByName("Canvas");
                // bgNode.removeChild(this.cow_ins);
                this.node.removeChild(this.cow_ins);
                //跟换绳子
                let ropeType = this.cow_ins.getComponent("cow").randomType+1;
                this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_images[ropeType];
                //生成新的牛节点
                this.cow_ins = cc.instantiate(this.cow_prefab);
                this.node.addChild(this.cow_ins);
                //捕获成功
                this.success = true;
                this.scoreNum ++;
            }else {
                cc.log("捕获失败")
            }
        }, this);
        //往回拉
        let down = cc.moveTo(0.5,this.rope_node.x,-820);
        //重置绳子
        let finish = cc.callFunc(function () {
            // this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_images[0];
            //判断是否捕获成功
            if(this.success){
                let scoreLabel = cc.find("Canvas/Score").getComponent(cc.Label);
                scoreLabel.string = "得分:"+this.scoreNum;
                this.success = false;
            }
        },this);

        let sequence = cc.sequence(up,result,down,finish);
        this.rope_node.runAction(sequence);
        //还原绳子
        this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_images[0];
    },
    //关闭按钮，结束游戏，下一把
    closeBtn(){
        //游戏继续
        cc.director.resume();
        //重新加载整个场景
        cc.director.loadScene("game");
    },
    //分享按钮
    shareBtn(){
        wx.shareAppMessage({
            title : "荒岛套牛小游戏",
            imageUrl : "https://img.3dmgame.com/uploads/allimg/150127/218_150127192436_1_lit.jpg",
            success(res){
                console.log(res);
            },
            fail(res){
                console.log(res);
            }
        });
    },

});
